
var app = getApp()
Page({
  data: {
    noscoreUrl: '../../images/icon_noscore.png',
    scoredUrl: '../../images/icon_scored.png',
    isShowModal: false,
    bigImgShow: {},
    shopFhScore: 0,
    shopFwScore: 0,
    temPaths: [],
    
  },
  onLoad: function (e) {
    console.log('onLoad');
    var that = this;
    // that.setData({
    //   orderid: e.orderid
    // })
    // console.log(that.data.orderid)
    var tid = e.orderid;
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_order_comment',
        tid: tid
      },
      success: function (res) {
        if (res.data.ec == 200) {
          var orderinfo = res.data.data;
          for (var i = 0; i <orderinfo.goods.length; i++) {
           orderinfo.goods[i].commentTxt = "";
           orderinfo.goods[i].score = 0;
           orderinfo.goods[i].imgArr = [];
           orderinfo.goods[i].picsShow = [];
          }
          that.setData({
            orderinfo: orderinfo
          })
          console.log(that.data.orderinfo);
        } else {
          console.log(res.data);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  onShow: function () {
    var that = this;
    var tid = that.data.orderid;
  },
  hideModal: function () {
    this.setData({
      isShowModal: false
    })
  },
  peiviewImg: function (e) {
    var curimg = e.currentTarget.dataset.curimg;
    var imgs = e.currentTarget.dataset.imgs;
    wx.previewImage({
      current: curimg, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  goodScore: function (e) {
    var that = this;
    var gid = e.target.dataset.gid;
    var score = e.target.dataset.score;
    var orderinfo = that.data.orderinfo;
    for (var i = 0; i < orderinfo.goods.length; i++) {
      if (orderinfo.goods[i].gid == gid) {
        orderinfo.goods[i].score = score;
      }
    }
    that.setData({
      orderinfo: orderinfo
    })
    console.log(gid + "/" + score);
  },
  commentChange: function (e) {
    var that = this;
    var gid = e.target.dataset.gid;
    var orderinfo = that.data.orderinfo;
    for (var i = 0; i < orderinfo.goods.length; i++) {
      if (orderinfo.goods[i].gid == gid) {
        orderinfo.goods[i].commentTxt = e.detail.value;
      }
    }
    that.setData({
      orderinfo: orderinfo
    })
  },
  shopScore: function (e) {
    var that = this;
    var type = e.target.dataset.type;
    if (type == 'Fh') {
      var score = e.target.dataset.score;
      console.log(score);
      that.setData({
        shopFhScore: score
      })
    } else if (type == 'Fw') {
      var score = e.target.dataset.score;
      console.log(score);
      that.setData({
        shopFwScore: score
      })
    }
  },
  chooseImage: function (e) {
    console.log(e);
    var that = this;
    var gid = e.currentTarget.dataset.gid;
    var orderinfo = that.data.orderinfo;
    for (var i = 0; i < orderinfo.goods.length; i++) {
      if (orderinfo.goods[i].gid == gid) {
        var picsShow = orderinfo.goods[i].picsShow;
        var picsShowLength = 0;
        if (picsShow.length < 5) {
          picsShowLength = 5 - picsShow.length;
        } else {
          picsShowLength = 0;
        }
        console.log(picsShowLength);
        app.chooseImageTap(this, picsShowLength, i);
      }
    }
    
  },
  uploadImage: function (index) {//这里触发图片上传的方法
    var that = this;
    var temPaths = that.data.temPaths;
    var orderinfo = that.data.orderinfo;
    var imgArr = orderinfo.goods[index].imgArr;
    var picsShow = orderinfo.goods[index].picsShow;
    that.uploadimg({
      url: app.globalData.requestUrl + '&map=applet_img_upload',//这里是你图片上传的接口
      path: temPaths//这里是选取的图片的地址数组
    }, imgArr, picsShow,index);
  },
  uploadimg: function (data, imgArr, picsShow,index) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    var orderinfo = that.data.orderinfo;
    wx.showLoading({
      title: '正在上传',
    })
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'image',
      success: (resp) => {
        success++;
        var data = JSON.parse(resp.data);
        var curpath = data.data.path;
        var realpath = "https://shopping.zhjl.link" + data.data.path;
        console.log(curpath);
        console.log(realpath);
        // picsShow = realpath;
        // imgArr = curpath;
        picsShow.push(realpath);
        imgArr.push(curpath);
        console.log(orderinfo.goods);
      },
      fail: (res) => {
        fail++;
      },
      complete: () => {
        i++;
        if (i == data.path.length) {   //当图片传完时，停止调用        
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          console.log("dfs////////////");
          console.log(orderinfo);
          console.log(index);
          console.log(imgArr);
          console.log(picsShow);
          orderinfo.goods[index].imgArr = imgArr;
          orderinfo.goods[index].picsShow = picsShow;
          that.setData({
            orderinfo: orderinfo
          })
          wx.hideLoading();
          console.log("dfs////////////");
        } else {//若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data, imgArr, picsShow,index);
        }
      }
    });
  },
  delUploadImg: function (e) {
    var that = this;
    var gid = e.target.dataset.gid;
    var index = e.target.dataset.index;
    var orderinfo = that.data.orderinfo;
    for (var i = 0; i < orderinfo.goods.length; i++) {
      if (orderinfo.goods[i].gid == gid) {
        orderinfo.goods[i].imgArr.splice(index, 1);
        orderinfo.goods[i].picsShow.splice(index, 1);
      }
    }
    that.setData({
      orderinfo: orderinfo
    })
    console.log(gid);
    console.log(index);
  },
  saveEvaluate: function (e) {
    var that = this;
    var orderInfo = that.data.orderinfo;
    var data = {};
    var goods = [];
    data.map = 'applet_order_comment_save';
    // data.suid = app.globalData.suid;
    data.tid = orderInfo.tid;
    data.speed = that.data.shopFhScore;
    data.attitude = that.data.shopFwScore;
    for (var i = 0; i < orderInfo.goods.length; i++) {
      var obj = {};
      obj.toid = orderInfo.goods[i].toid;
      if (orderInfo.goods[i].score>0){
        obj.score = orderInfo.goods[i].score;
      }else{
        app.errorTip(that, "商品评分不能为空", 2000);
        return;
      }
      if (orderInfo.goods[i].commentTxt!=""){
        obj.content = orderInfo.goods[i].commentTxt;
      } else {
        app.errorTip(that, "评价内容不能为空", 2000);
        return;
      }
      
      obj.image = orderInfo.goods[i].imgArr;
      console.log(obj);
      goods.push(obj)
    }
    // data.goods = encodeURI(JSON.stringify(goods));
    data.goods = goods;

    if (data.speed>0){
      if (data.attitude>0){
        console.log(data);
        //发起请求，获取列表列表
        wx.showToast({
          title: '加载中',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data.data);
              wx.showModal({
                title: '提示',
                content: res.data.data.msg,
                showCancel: false,
                success:function(){
                  wx.navigateBack({
                    delta: 1
                  })
                }
              });
            } else {
              console.log(res.data);
              app.errorTip(that, res.data.em, 2000);
            }
          },
          complete: function () {
            wx.hideToast();
          }
        });
      }else{
        app.errorTip(that, "店铺服务态度评分不能为空", 2000);
      }
    }else{
      app.errorTip(that, "店铺发货速度评分不能为空", 2000);
    }
  }
})